package com.timepies.activity;

import static com.timepies.database.Constants.FIELD_DATE;
import static com.timepies.database.Constants.FIELD_SUBJECT;
import static com.timepies.database.Constants.FIELD_TEXT;
import static com.timepies.database.Service.getInstance;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.timepies.R;
import com.timepies.activityutils.ElencoLeftMenu;
import com.timepies.activityutils.ElencoList;
import com.timepies.database.Service;
import com.timepies.domain.MenuPies;
import com.timepies.domain.Timepie;

public class ElencoPieActivity extends Activity {
	private static final String TAG = ElencoPieActivity.class.getSimpleName();
	private Dialog editDialog = null;
	private ElencoLeftMenu leftMenu;
	private ElencoList elencoPieList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pie_elenco);

		elencoPieList = new ElencoList(this);
		registerForContextMenu(elencoPieList.getList());

		leftMenu = new ElencoLeftMenu(this, MenuPies.titoli());

		Log.d(TAG, "onCreate complete!");

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.elenco_menu, menu);
		switch (getInstance(this).getMenuPies()) {

		case FESTIVITA:
		case PONTI:
		case SHAREDPIES:
			MenuItem item = menu.findItem(R.id.action_add);
			item.setVisible(false);
			break;
		case TIMEPIES:
			break;
		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Log.d(TAG, "MenuItem: " + item.getItemId());
		switch (item.getItemId()) {

		case R.id.action_settings:
			startActivity(new Intent(ElencoPieActivity.this,
					SettingsActivity.class));
			finish();
			break;
		case R.id.action_about:
			startActivity(new Intent(ElencoPieActivity.this,
					SplashActivity.class));
			finish();
			break;

		case R.id.action_add:
			Intent intent = new Intent(this, ModificaPieActivity.class);
			intent.putExtra("caller", getClass());
			startActivity(intent);
			finish();
			break;

		default:
			leftMenu.select(item);

		}

		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		MenuInflater inflater = getMenuInflater();

		switch (getInstance(this).getMenuPies()) {
		case FESTIVITA:
		case PONTI:
			break;
		case SHAREDPIES:
			super.onCreateContextMenu(menu, v, menuInfo);
			inflater.inflate(R.menu.dettaglio_context_menu, menu);
			MenuItem item = menu.findItem(R.id.action_share);
			item.setVisible(false);
			break;
		case TIMEPIES:
			super.onCreateContextMenu(menu, v, menuInfo);
			inflater.inflate(R.menu.dettaglio_context_menu, menu);
			break;
		}

	}

	@Override
	public boolean onContextItemSelected(MenuItem i) {
		Service service = getInstance(this);
		switch (i.getItemId()) {
		case R.id.action_share:
			service.saveAsyncTimepie(getFromCursor(i));
			Toast.makeText(this, "Evento Condiviso.", Toast.LENGTH_LONG).show();
			return true;
		case R.id.action_delete:
			if (service.getMenuPies() == MenuPies.SHAREDPIES)
				service.deleteAsync(getFromList(i));
			if (service.getMenuPies() == MenuPies.TIMEPIES)
				service.delete(getFromCursor(i).getId());
			elencoPieList = new ElencoList(this);
			Toast.makeText(this, "Evento Eliminato.", Toast.LENGTH_LONG).show();
			return true;

		}
		return false;
	}

	public Timepie getFromCursor(MenuItem item) {
		try {
			BaseAdapter adapter = elencoPieList.getAdapter();
			int position = ((AdapterContextMenuInfo) item.getMenuInfo()).position;
			long id = adapter.getItemId(position);
			Cursor c = (Cursor) adapter.getItem(position);
			String testo = c.getString(c.getColumnIndex(FIELD_SUBJECT));
			String oggetto = c.getString(c.getColumnIndex(FIELD_TEXT));
			String data = c.getString(c.getColumnIndex(FIELD_DATE));
			return new Timepie(id, data, oggetto, testo);
		} catch (Exception e) {
			Log.e(toString(), e.getMessage());
		}
		return null;
	}

	public Timepie getFromList(MenuItem item) {
		Timepie timepie = null;
		try {
			BaseAdapter adapter = elencoPieList.getAdapter();
			int position = ((AdapterContextMenuInfo) item.getMenuInfo()).position;
			timepie = (Timepie) adapter.getItem(position);
		} catch (Exception e) {
			Log.e(toString(), e.getMessage());
		}
		return timepie;
	}

	static String VOWEL_GROUP = "AEIOUaeiou";

	@Override
	public void onBackPressed() {
		Builder finishDialog = new Builder(this);
		finishDialog.setTitle("Abbandona Pie");
		finishDialog.setMessage("Sicuro di voler uscire?");
		finishDialog.setPositiveButton("Si",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int button) {
						ElencoPieActivity.this.finish();
					}
				});
		finishDialog.setNegativeButton("No", null).show();
	}

	public Dialog getEditDialog() {
		return editDialog;
	}

	public ElencoList getElencoPieList() {
		return elencoPieList;
	}

}